package com.jeesite.modules.isys.minio;


import com.jeesite.modules.isys.common.entity.Res;
import io.minio.messages.Bucket;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;

@Api(tags = "文件相关接口")
@Slf4j
@RestController
@RequestMapping(value = "${adminPath}/product/file")
public class FileController {

    @Autowired
    private MinioUtils minioUtil;
    @Autowired
    private MinioConfig prop;

    @ApiOperation(value = "查看存储bucket是否存在")
    @GetMapping("/bucketExists")
    public Res bucketExists(@RequestParam("bucketName") String bucketName) {
        return Res.ok(minioUtil.bucketExists(bucketName));
    }

    @ApiOperation(value = "创建存储bucket")
    @GetMapping("/makeBucket")
    public Res makeBucket(String bucketName) {
        return Res.ok(minioUtil.makeBucket(bucketName));
    }

    @ApiOperation(value = "删除存储bucket")
    @GetMapping("/removeBucket")
    public Res removeBucket(String bucketName) {
        return Res.ok(minioUtil.removeBucket(bucketName));
    }

    @ApiOperation(value = "获取全部bucket")
    @GetMapping("/getAllBuckets")
    public Res getAllBuckets() {
        List<Bucket> allBuckets = minioUtil.getAllBuckets();
        return Res.ok(allBuckets);
    }

    @ApiOperation(value = "文件上传返回url")
    @PostMapping("/upload")
    public Res upload(@RequestParam("file") MultipartFile file) {
        String objectName = minioUtil.upload(file);
        if (null != objectName) {
            HashMap<String,String> hashMap = new HashMap<>();
            String preview = minioUtil.preview(objectName);
            hashMap.put("url",preview);
            return Res.ok(hashMap);
        }
        return Res.error();
    }

    @ApiOperation(value = "图片/视频预览")
    @GetMapping("/preview")
    public Res preview(@RequestParam("fileName") String fileName) {
        HashMap<String,String> hashMap = new HashMap<>();
        hashMap.put("filleName",minioUtil.preview(fileName));
        return Res.ok(hashMap);
    }


    //构造的文件下载地址
//    http://localhost:8980/js/product/file/download?fileName=32c87b0c-63b3-497b-8a83-27fb0c6f668c.pdf
    @ApiOperation(value = "文件下载")
    @GetMapping("/download")
    public Res download(@RequestParam("fileName") String fileName, HttpServletResponse res) {
        minioUtil.download(fileName,res);
        return Res.ok("成功");
    }

    @ApiOperation(value = "删除文件", notes = "根据url地址删除文件")
    @PostMapping("/delete")
    public Res remove(String url) {
        String objName = url.substring(url.lastIndexOf(prop.getBucketName()+"/") + prop.getBucketName().length()+1);
        minioUtil.remove(objName);
        HashMap<String,String> hashMap = new HashMap<>();
        hashMap.put("objName",objName);
        return Res.ok(hashMap);
    }



}
